//
//  RegisterInputVM.swift
//  kxf
//
//  Created by Mac on 2024/3/4.
//

import Foundation
import UIKit
import MCToast

class RegisterInputVM: UIView {
    
    let selfHeight = kFitWidth(56)
    var maxLength = 20
    
    var getCodeBlock:(()->())?
    var getCodeTime = 60
    
    var tapBlock:(()->())?

    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
        self.backgroundColor = .white
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var lineView : UIView = {
        let vi = UIView()
        vi.backgroundColor = .COLOR_LINE_GREY
        
        return vi
    }()
    lazy var leftLabel : UILabel = {
        let lab = UILabel()
        lab.textColor = .black
        lab.font = .systemFont(ofSize: 14, weight: .medium)
        lab.adjustsFontSizeToFitWidth = true
        
        return lab
    }()
    lazy var textField : UITextField = {
        let text = UITextField()
        text.keyboardType = .asciiCapable
        text.delegate = self
        text.font = .systemFont(ofSize: 15, weight: .regular)
        
        return text
    }()
    lazy var rightLabel : UILabel = {
        let lab = UILabel()
        lab.font = .systemFont(ofSize: 15, weight: .regular)
        lab.textColor = .COLOR_TEXT_BLACK333
        lab.numberOfLines = 2
        lab.lineBreakMode = .byCharWrapping
        lab.isHidden = true
        lab.isUserInteractionEnabled = true
        return lab
    }()
    lazy var getCodeBtn : UIButton = {
        let btn = UIButton()
        btn.setTitle("获取验证码", for: .normal)
        btn.setTitleColor(UIColor.THEME, for: .normal)
        btn.titleLabel?.font = .systemFont(ofSize: 14, weight: .regular)
        
        btn.addTarget(self, action: #selector(getCodeAction), for: .touchUpInside)
        
        return btn
    }()
    lazy var arrowImgView : UIImageView = {
        let img = UIImageView()
        img.image = UIImage.init(named: "login_arrow_img")
        img.isUserInteractionEnabled = true
        
        return img
    }()
}

extension RegisterInputVM{
    @objc func getCodeAction() {
        if self.getCodeBlock != nil{
            self.getCodeBlock!()
        }
    }
    func showArrowImg() {
        addSubview(arrowImgView)
        
        arrowImgView.snp.makeConstraints { make in
            make.right.equalTo(kFitWidth(-18))
            make.width.height.equalTo(kFitWidth(20))
            make.centerY.lessThanOrEqualToSuperview()
        }
        textField.snp.remakeConstraints { make in
            make.left.equalTo(kFitWidth(121))
            make.centerY.lessThanOrEqualToSuperview()
            make.right.equalTo(arrowImgView.snp.left).offset(kFitWidth(-20))
        }
        rightLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(121))
            make.centerY.lessThanOrEqualToSuperview()
            make.right.equalTo(arrowImgView.snp.left).offset(kFitWidth(-20))
        }
        textField.isEnabled = false
//        textField.isHidden = true
        rightLabel.isHidden = false
        
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(tapAction))
        self.addGestureRecognizer(tap)
    }
    func setLabelContent(content:String){
        if content.count > 0 {
            rightLabel.text = content
            textField.isHidden = true
        }
    }
    @objc func tapAction() {
        if self.tapBlock != nil{
            self.tapBlock!()
        }
    }
}
extension RegisterInputVM{
    func initUI() {
        addSubview(lineView)
        addSubview(leftLabel)
        addSubview(textField)
        addSubview(rightLabel)
        
        setConstrait()
    }
    func setConstrait() {
        lineView.snp.makeConstraints { make in
            make.left.top.width.equalToSuperview()
            make.height.equalTo(kFitWidth(1))
        }
        leftLabel.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(24))
            make.centerY.lessThanOrEqualToSuperview()
            make.width.equalTo(kFitWidth(90))
        }
        textField.snp.makeConstraints { make in
            make.left.equalTo(kFitWidth(121))
            make.centerY.lessThanOrEqualToSuperview()
            make.right.equalTo(kFitWidth(-20))
        }
    }
    
    func showCodeBtn(){
        addSubview(getCodeBtn)
        getCodeBtn.snp.makeConstraints { make in
            make.centerY.lessThanOrEqualToSuperview()
            make.right.equalTo(kFitWidth(-20))
            make.width.equalTo(kFitWidth(80))
            make.height.equalTo(kFitWidth(26))
        }
        textField.snp.remakeConstraints { make in
            make.left.equalTo(kFitWidth(121))
            make.centerY.lessThanOrEqualToSuperview()
            make.right.equalTo(getCodeBtn.snp.left).offset(kFitWidth(-20))
        }
    }
}


extension RegisterInputVM:UITextFieldDelegate{
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == "" {
            return true
        }
        if textField.text?.count ?? 0 > self.maxLength-1{
            return false
        }
        return true
    }
}
